let ctx=wx.createCanvasContext('myCanvas')
Page({
    data:{
        mosaic:true,
        imagePath:'',
    },
    
    open(){
        wx.chooseImage({
            count:1,
        }).then(res=>{
            console.log(res)
            let file=res.tempFilePaths[0]
            ctx.drawImage(file,0,0,420,500)
            ctx.draw()
        })
    },

    clear(){
        this.setData({
            mosaic:false
        })
    },

    mosaic(){
        this.setData({
            mosaic:true
        })
    },

    move(e){
        console.log(e)
        if(this.data.mosaic){
            ctx.setFillStyle('black')
            ctx.fillRect(e.touches[0].x,e.touches[0].y,10,10)
            ctx.fillRect(e.touches[0].x+10,e.touches[0].y+10,10,10)
            ctx.setFillStyle('gray')
            ctx.fillRect(e.touches[0].x+10,e.touches[0].y,10,10)
            ctx.fillRect(e.touches[0].x,e.touches[0].y+10,10,10)
        }else{
            ctx.clearRect(e.touches[0].x,e.touches[0].y,20,20)
        }
        ctx.draw(true)
    },

    save(){
        wx.canvasToTempFilePath({
          canvasId: 'myCanvas',
        }).then(res=>{
            console.log('savefile',res)
            this.setData({
                imagePath:res.tempFilePath
            })
            wx.saveImageToPhotosAlbum({
                filePath: res.tempFilePath,
                success(res) {
                  wx.showToast({
                    title: '保存图片成功！',
                  })
                },
                fail(res) {
                  wx.showToast({
                    title: '保存图片失败！',
                  })
                }
              })
        })
    },

    seal(){
        ctx.setFillStyle('red')
        ctx.setFontSize(36)
        ctx.fillText('07200845谢瑞辉',0,200)
        ctx.draw(true)
    }
})